package com.example.volunteer.service.impl;

import com.example.volunteer.exception.CustomException;
import com.example.volunteer.mapper.ActivityMapper;
import com.example.volunteer.mapper.VolunteerMapper;
import com.example.volunteer.model.Activity;
import com.example.volunteer.model.Volunteer;
import com.example.volunteer.service.NotificationService;
import com.example.volunteer.service.SmsSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsNotificationServiceImpl implements NotificationService {

    @Autowired
    private VolunteerMapper volunteerMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SmsSender smsSender;

    @Override
    public void sendApprovalNotification(String volunteerId, Integer activityId) {
        Volunteer volunteer = volunteerMapper.findById(volunteerId);
        if (volunteer == null) {
            throw new CustomException("志愿者不存在");
        }
        Activity activity = activityMapper.findById(activityId);
        if (activity == null) {
            throw new CustomException("活动不存在");
        }

        String message = String.format(
                "亲爱的%s，您报名的活动【%s】已通过审核。活动时间：%s，地点：%s。请准时参加！",
                volunteer.getName(),
                activity.getTitle(),
                activity.getStartTime(),
                activity.getAddress()
        );

        smsSender.sendSms(volunteer.getContact(), message);
    }

    @Override
    public void sendRejectionNotification(String volunteerId, Integer activityId) {
        Volunteer volunteer = volunteerMapper.findById(volunteerId);
        if (volunteer == null) {
            throw new CustomException("志愿者不存在");
        }

        Activity activity = activityMapper.findById(activityId);
        if (activity == null) {
            throw new CustomException("活动不存在");
        }

        String message = String.format(
                "亲爱的%s，您报名的活动【%s】未通过审核。感谢您的参与！",
                volunteer.getName(),
                activity.getTitle()
        );

        smsSender.sendSms(volunteer.getContact(), message);
    }

    @Override
    public void sendReminderNotification(String volunteerId, Integer activityId) {
        // 实现活动提醒逻辑
    }
}
